# WVP数据库连接诊断脚本
Write-Host "=== WVP数据库连接诊断 ===" -ForegroundColor Green

# 1. 检查MySQL服务状态
Write-Host "`n1. 检查MySQL服务状态..." -ForegroundColor Yellow
try {
    $mysqlService = Get-Service -Name "*mysql*" -ErrorAction SilentlyContinue
    if ($mysqlService) {
        foreach ($service in $mysqlService) {
            Write-Host "  服务名: $($service.Name) - 状态: $($service.Status)" -ForegroundColor $(if($service.Status -eq 'Running'){'Green'}else{'Red'})
        }
    } else {
        Write-Host "  未找到MySQL服务" -ForegroundColor Red
    }
} catch {
    Write-Host "  检查MySQL服务时出错: $($_.Exception.Message)" -ForegroundColor Red
}

# 2. 检查MySQL端口
Write-Host "`n2. 检查MySQL端口3306..." -ForegroundColor Yellow
try {
    $portCheck = Test-NetConnection -ComputerName "127.0.0.1" -Port 3306 -WarningAction SilentlyContinue
    if ($portCheck.TcpTestSucceeded) {
        Write-Host "  ✓ 端口3306可访问" -ForegroundColor Green
    } else {
        Write-Host "  ❌ 端口3306不可访问" -ForegroundColor Red
    }
} catch {
    Write-Host "  检查端口时出错: $($_.Exception.Message)" -ForegroundColor Red
}

# 3. 检查网络连接
Write-Host "`n3. 检查网络连接..." -ForegroundColor Yellow
try {
    $netstat = netstat -an | findstr ":3306"
    if ($netstat) {
        Write-Host "  ✓ 发现MySQL监听端口:" -ForegroundColor Green
        $netstat | ForEach-Object { Write-Host "    $_" -ForegroundColor White }
    } else {
        Write-Host "  ❌ 未发现MySQL监听端口3306" -ForegroundColor Red
    }
} catch {
    Write-Host "  检查网络连接时出错: $($_.Exception.Message)" -ForegroundColor Red
}

# 4. 检查MySQL进程
Write-Host "`n4. 检查MySQL进程..." -ForegroundColor Yellow
try {
    $mysqlProcess = Get-Process -Name "*mysql*" -ErrorAction SilentlyContinue
    if ($mysqlProcess) {
        foreach ($process in $mysqlProcess) {
            Write-Host "  ✓ 进程: $($process.Name) (PID: $($process.Id))" -ForegroundColor Green
        }
    } else {
        Write-Host "  ❌ 未找到MySQL进程" -ForegroundColor Red
    }
} catch {
    Write-Host "  检查MySQL进程时出错: $($_.Exception.Message)" -ForegroundColor Red
}

# 5. 尝试连接测试
Write-Host "`n5. 尝试连接测试..." -ForegroundColor Yellow
Write-Host "  正在尝试连接 mysql://127.0.0.1:3306..." -ForegroundColor White

# 创建简单的连接测试
$testScript = @"
import java.sql.*;
public class TestConnection {
    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1:3306/?useSSL=false&allowPublicKeyRetrieval=true";
        String username = "root";
        String password = "12345678";
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, username, password);
            System.out.println("✓ 数据库连接成功!");
            
            // 检查数据库
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SHOW DATABASES LIKE 'wvp'");
            if (rs.next()) {
                System.out.println("✓ 数据库 'wvp' 存在");
            } else {
                System.out.println("❌ 数据库 'wvp' 不存在");
            }
            
            conn.close();
        } catch (Exception e) {
            System.out.println("❌ 连接失败: " + e.getMessage());
        }
    }
}
"@

$testScript | Out-File -FilePath "TestConnection.java" -Encoding UTF8

Write-Host "`n=== 诊断结果总结 ===" -ForegroundColor Cyan
Write-Host "请检查以上诊断结果，常见问题和解决方案：" -ForegroundColor White

Write-Host "`n问题1: MySQL服务未运行" -ForegroundColor Yellow
Write-Host "解决方案: 启动MySQL服务" -ForegroundColor White
Write-Host "  - Windows服务: services.msc -> 找到MySQL -> 启动" -ForegroundColor Gray
Write-Host "  - 命令行: net start mysql" -ForegroundColor Gray

Write-Host "`n问题2: 端口3306被占用或配置错误" -ForegroundColor Yellow
Write-Host "解决方案: 检查MySQL配置文件my.ini中的端口设置" -ForegroundColor White

Write-Host "`n问题3: 用户名密码错误" -ForegroundColor Yellow
Write-Host "解决方案: 重置MySQL root密码" -ForegroundColor White
Write-Host "  - 停止MySQL服务" -ForegroundColor Gray
Write-Host "  - 使用 --skip-grant-tables 启动" -ForegroundColor Gray
Write-Host "  - 重置密码后重启服务" -ForegroundColor Gray

Write-Host "`n问题4: 数据库wvp不存在" -ForegroundColor Yellow
Write-Host "解决方案: 执行数据库初始化脚本" -ForegroundColor White
Write-Host "  - 使用 fix_database_now.sql 脚本" -ForegroundColor Gray

Write-Host "`n下一步操作建议：" -ForegroundColor Green
Write-Host "1. 确保MySQL服务正在运行" -ForegroundColor White
Write-Host "2. 验证连接参数（主机、端口、用户名、密码）" -ForegroundColor White
Write-Host "3. 执行数据库初始化脚本" -ForegroundColor White
Write-Host "4. 重新启动WVP应用程序" -ForegroundColor White

Read-Host "`n按回车键退出"
